package com.example.test.bulider;

import cn.hutool.json.JSONUtil;
import com.example.test.bean.Tree;
import com.example.test.bean.User;
import com.example.test.bulider.director.UserDirector;
import com.example.test.bulider.impl.UserBuilerServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;


public class main {

    public static void main(String[] args) {
        UserBuilerService userBuilerService = new UserBuilerServiceImpl();
        new UserDirector(userBuilerService).build();
        User build = userBuilerService.build();

        System.out.println(JSONUtil.toJsonStr(build));


        List<Tree> list = new main().initList();
        System.out.println(list.size());


    }

    private List<Tree> initList() {
        List<Tree> trees = new ArrayList<>();
        trees.add(new Tree("0", "root"));
        trees.add(new Tree("1", "0"));
        trees.add(new Tree("2", "0"));
        trees.add(new Tree("6", "2"));
        trees.add(new Tree("7", "6"));
        trees.add(new Tree("3", "1"));
        trees.add(new Tree("4", "1"));
        trees.add(new Tree("5", "4"));
        trees.add(new Tree("7", "6"));
        trees.add(new Tree("5", "4"));
        List<Tree> collect = trees.stream().filter(distinctByKey(tree -> tree.getId())).collect(Collectors.toList());
        System.out.println(collect.size());
        return trees;
    }

    private <T> Predicate<T> quchong(Function<T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }

}
